// 物流基类
module.exports = Behavior({
  properties: {},
  data: {

    host: 'https://www.yunanhua.com',
    imgHost: 'https://img.yunanhua.com',
    uid:"",
	  toLogin:true,//跳转商家登录
    userInfo: { //用户数据

    },

  },

  ready: function () {

  },

  methods: {
    onLoad: function (options) {

    },

    /**
     * 获取完整的缩略图地址
     * @param {string} url 
     * @param {string} type  缩略图大小，默认small,中图wide
     */
    get_face(url, type = 'small') {
      let thumb = '/assets/common/logo.png';
      if (url) {
        thumb = this.data.imgHost + '/' + url + '!' + type;
      }
      return thumb
    },



    /**
     * 获取完整的缩略图地址
     * @param {string} url 
     * @param {string} type  缩略图大小，默认small,中图wide
     */
    get_thumb(url, type = 'small') {
      let thumb = '/assets/common/logo.png';
      if (url) {
        thumb = this.data.imgHost + '/' + url + '!' + type;
      }
      return thumb
    },

    /**显示弹窗通用方法 */
    show_popup(e) {
      let obj = {};
      obj[e.currentTarget.dataset.field] = true;
      this.setData(obj)
    },
    /**隐藏弹窗通用方法 */
    hide_popup(e) {
      let obj = {};
      obj[e.currentTarget.dataset.field] = false;
      this.setData(obj)
    },
    /**
     * 网络请求封装
     * @param {*} req_data  
     * url 请求地址,
     * loadingTip:加载中提示，如果存在，先显示loading
     * @param {*} success_func 成功回调方法
     * @param {*} fail_func 失败回调方法,code 不为零时也会调用
     * @param {*} complete 最终执行的方法
     */
    request: function (req_data, success_func, fail_func = null, complete = null) {
      let that = this;
      let Authorization = wx.getStorageSync('Authorization');

      if (req_data.loadingTip) {
        wx.showLoading({
          title: req_data.loadingTip,
          mask: true
        })
      }
      req_data.header = {
        'content-type': 'application/json',
        'Accept': 'application/json, text/javascript, */*; q=0.01'
      };
      req_data.header.client = 'express';

      let authToken = wx.getStorageSync('authToken');
      req_data.header.authtoken = authToken;
      if (Authorization) {
        req_data.header.Authorization = Authorization;
      }
      req_data.success = function (res) { //成功处理
        // console.log("res.statusCode", res.statusCode);
        if (res.statusCode == 200) {
          if (res.header.Authorization) {
            wx.setStorage({
              data: res.header.Authorization,
              key: 'Authorization',
            })
          }
          let data = res.data;
          if (data.code == 0) {
            success_func(data.result || data);
          } else {
            if (fail_func) {
              return fail_func(data);
            }
            wx.showModal({
              showCancel: false,
              content: data.result || data.msg + "" || data.message + "",
            })

          }
        } else if (res.statusCode == 401) {
          //进行登陆
          wx.showModal({
            content: '请先授权登陆',
            showCancel: true,
            title: '登陆提示',
            success: (result) => {
              if (result.confirm) {
                console.log("invite_id2", that.data.uid);
                wx.login({
                  success: (loginRes) => {
                    // console.log("loginRes",loginRes);
                    //code 登陆
                    wx.request({
                      url: that.data.host + "/home/login?code=" + loginRes.code+"&invite_id="+that.data.uid,
                      success: (res1) => {
                        let user = res1.data;
                        // console.log("res1",res1);
                        wx.showToast({
                          title: '登陆成功',
                        })
                        wx.setStorageSync('Authorization', user.data.access_token)
                        that.data.userInfo = getApp().globalData.userInfo = user.data;
                        console.log("that.userInfo.audit", that.data.userInfo);
                        if (!that.data.userInfo.audit) {
                          console.log("that.getUserProfile()");
                          that.getUserProfile();
                        }
                        that.login_reload()
                      },
                      fail: (e) => {
                        // console.log(e);
                        wx.showToast({
                          title: e.errMsg || '网络异常',
                          icon: 'none'
                        })
                      },
                    })
                  },
                  fail: (e) => {
                    // console.log("wx.login失败", e);
                    wx.showToast({
                      title: e.errMsg || '没有授权成功',
                      icon: 'none'
                    })
                  },
                })
              }
            },
          })
        } else if (res.statusCode == 406) {
          wx.showToast({
            title: '请重新登陆',
            icon: 'none'
          });
          wx.redirectTo({
            url: '/pages/cls/staff/Login',
          })

        } else if (res.statusCode == 402){//商家未登录状态，跳转至登录页
			if(that.data.toLogin){
				that.setData({
					toLogin:false
				})
				wx.showModal({
				  content: '请重新登陆',
				  showCancel: true,
				  title: '登陆提示',
				  success: (result) => {
				    if (result.confirm) {
						that.setData({
							toLogin:true
						})
				      wx.navigateTo({
				        url: '/pages/business/login/index',
				      })
				    }
				  },
				})
			}
		}else {
          // console.log("res.statusCode", res.statusCode);
          if (fail_func) {
            return fail_func(res);
          }
          wx.showModal({
            showCancel: false,
            content: res.data.message || res.data || "网络异常",
          })

        }
      };
      req_data.fail = function (e) { //失败处理
        // console.log("req_data.fail".e)
        fail_func ? fail_func(e) : wx.showModal({
          showCancel: false,
          content: '网络异常：' + e.errMsg
        });

      }
      req_data.complete = function (e) { //失败处理
        req_data.loadingTip ? wx.hideLoading() : '';
        complete ? complete(e) : '';
      }
      wx.request(req_data);
    },

  }
})